package src.controller;

import java.rmi.RemoteException;
import java.util.Set;

import javax.swing.DefaultListModel;


import src.domain.PostOffice;
import src.resource.CommonMethods;
import src.resource.DialogResource;
import src.resource.DialogResource.windowTypeEnum;
import src.service.ClientService;
import src.view.ManageTrustRelationships;

public class ManageTrustRelationshipsController {
	
	private ClientService system;
	private ManageTrustRelationships view;
	

	public ManageTrustRelationshipsController(ClientService system) {
		this.view = ManageTrustRelationships.getInstance(this);
		this.system = system;
	}
	
	public void performSearchPostOffices() throws RemoteException{
		
		this.view.getListModelPostOffices().removeAllElements();
		view.setListModelPostOffices(toListModel(system.getPostOffices(view.getTxtSearch().getText())));
		view.getjListPostOffice().setModel(view.getListModelPostOffices());
		view.getListModelAddedPostOffices().removeAllElements();
		view.getListModelAvailablePostOffices().removeAllElements();
		view.getTxtSearch().setText("");
	}
	
	private DefaultListModel toListModel(Set<PostOffice> set){
		DefaultListModel l=new DefaultListModel();
		for(PostOffice p:set)
			l.addElement(p);
		return l;
	}
	
	public void performSelectItem() throws RemoteException{
		if(view.getjListPostOffice().getSelectedValue()==null)
			return;
		view.setListModelAddedPostOffices(toListModel(((PostOffice)view.getjListPostOffice().getSelectedValue()).getTrustOffices()));
		view.getjListAddedPostOffices().setModel(view.getListModelAddedPostOffices());
		view.setListModelAvailablePostOffices(toListModel(system.getAvailablePostOffices((PostOffice)view.getjListPostOffice().getSelectedValue())));
		view.getjListAvailablePostOffices().setModel(view.getListModelAvailablePostOffices());
		
	}
	
	public void performAdd(){
		if(this.view.getjListAvailablePostOffices().getSelectedValue()==null)
			DialogResource.showMessage(view.getBtnAdd().getLocationOnScreen(),"Information","Must select a item to add.",windowTypeEnum.wtInformation);
		this.view.getListModelAddedPostOffices().addElement(view.getjListAvailablePostOffices().getSelectedValue());
		this.view.getListModelAvailablePostOffices().removeElement(view.getjListAvailablePostOffices().getSelectedValue());
	}
	
	public void performRemove(){
		if(this.view.getjListAddedPostOffices().getSelectedValue()==null)
			DialogResource.showMessage(view.getBtnAdd().getLocationOnScreen(),"Information","Must select a item to remove.",windowTypeEnum.wtInformation);
		this.view.getListModelAvailablePostOffices().addElement(view.getjListAddedPostOffices().getSelectedValue());
		this.view.getListModelAddedPostOffices().removeElement(view.getjListAddedPostOffices().getSelectedValue());
	}
	
	public void  performUpdatePostOffice() throws RemoteException{
		if(this.view.getjListPostOffice().getSelectedValue()==null){
			DialogResource.showMessage(view.getBtnSave().getLocationOnScreen(),"Information","Must select a Post office.",windowTypeEnum.wtInformation);
			return;
		}
		((PostOffice)this.view.getjListPostOffice().getSelectedValue()).setTrustOffices(CommonMethods.toSet(view.getListModelAddedPostOffices()));
		system.updatePostOffice((PostOffice)this.view.getjListPostOffice().getSelectedValue());
		DialogResource.showMessage(view.getBtnSave().getLocation(),"Information","Post Office saved sucessfully.",windowTypeEnum.wtConfirmation);
		view.getListModelPostOffices().removeAllElements();
		view.getListModelAddedPostOffices().removeAllElements();
		view.getListModelAvailablePostOffices().removeAllElements();
		view.getTxtSearch().setText("");
	}
	
}
